<template>
  <div>
    <!-- 树形控件 -->
    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      check-strictly
      default-expand-all
      :default-checked-keys="permIds"
      node-key="id"
    />
    <el-row type="flex" justify="end">
      <el-button type="info" @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </el-row>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permissions'
import { tranListToTreeData } from '@/utils'
import { getRolesDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    rowId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      permIds: []
    }
  },
  created() {
    this.loadPermissionList()
    this.loadRolesDetail()
  },
  methods: {
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPerm({ id: this.rowId, permIds })
        this.$emit('close')
        console.log('assignPerm', res)
        // 保存数据
      } catch (err) {
        console.log('assignPerm', err)
      }
    },
    async loadRolesDetail() {
      try {
        const res = await getRolesDetail(this.rowId)
        this.permIds = res.data.permIds
        this.$refs.tree.setCheckedKeys(this.permIds)
        console.log('getRolesDetail', res)
        // 保存数据
      } catch (err) {
        console.log('getRolesDetail', err)
      }
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        this.list = tranListToTreeData(res.data, '0')
        console.log('getPermissionList', res)
      // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
